import com.alibaba.fastjson2.JSON;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class FileUploaderTest {

  @Test
  public void testUpload()
      throws IOException, NoSuchAlgorithmException, InvalidKeyException {
    try {
      // Create a minioClient with the MinIO server playground, its access key and secret key.
      MinioClient minioClient =
          MinioClient.builder()
              .endpoint("http://127.0.0.1:9000")
              .credentials("VVoM4NAfB6D9aPHbty5t", "T8TF2QKDuEheGM1icBo4iY0r3c2545rcpkRADJix")
              .build();

      // Make 'asiatrip' bucket if not exist.
      boolean found =
          minioClient.bucketExists(BucketExistsArgs.builder().bucket("img").build());
      if (!found) {
        // Make a new bucket called 'asiatrip'.
        minioClient.makeBucket(MakeBucketArgs.builder().bucket("img").build());
      } else {
        log.info("Bucket 'img' already exists.");
      }

      // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
      // 'asiatrip'.
      ObjectWriteResponse resp = minioClient.uploadObject(
          UploadObjectArgs.builder()
              .bucket("img")
              .object("memory_2.csv")
              .filename("C:\\Users\\Brant\\Downloads\\memory.csv")
              .build());
      log.info(
          "'C:\\Users\\Brant\\Downloads\\memory.csv' is successfully uploaded as "
              + "object 'memory_2.csv' to bucket 'img'.");
      log.info("upload resp is {}", JSON.toJSONString(resp));
    } catch (MinioException e) {
      log.error("Error occurred: " + e);
      log.error("HTTP trace: " + e.httpTrace());
    }
  }
}